package com.dst.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dst.entity.User;
import com.dst.service.UserService;
import com.dst.utli.JwtUtils;
import org.checkerframework.checker.units.qual.Temperature;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
  @Resource
  private UserService userService;
  @GetMapping("/login")
  public Map<String, String> login(@RequestBody User user){
    User user1 = userService.selectByNameAndPassword(user);
    Map<String, String> hashMap = new HashMap<>();
    if(user1!=null){
      hashMap.put("msg","登录成功");
      Map<String,String> map=new HashMap<>();
      map.put("name",user1.getName());
      map.put("pwd",user1.getPwd()+"");
      map.put("role",user1.getUserisadmin()+"");
      map.put("id",user1.getId()+"");
      String token = JwtUtils.getToken(map);
      hashMap.put("token",token);
    }else {
      hashMap.put("msg","登录失败");
    }
    return hashMap;
  }
  @GetMapping("/selectOne/{id}")
  public User selectOne(@PathVariable("id") Integer id){
    User byId = userService.getById(id);
    return byId;
  }
}
